<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
      <!-- 表格区域 -->
      <!-- data代表数据对象，prop代表列随营的数据属性名 -->
      <el-table stype="width: 100%;" :data="cateList" border stripe>
        <el-table-column label="序号" type="index" prop="id" width="100"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias">
          <!-- 作用域插槽 ，将el-table-column组件里的数据，赋值结构出row，（每一行的数据）-->
          <template v-slot="{row}">
            <span :style="{color:red}">{{row.cate_alias}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 作用域插槽 -->
            <el-button @click='handleEdit(scope.row.id)' type="primary" size="mini">修改</el-button>
            <el-button @click="remove(scope.row.id)" type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>

    <!-- 修改文章分类的对话框 -->
    <el-dialog @closed='edithandleReset' title="修改文章分类" :visible.sync="editVisible" width="35%">
      <!-- 修改的表单 -->
      <el-form :model="editForm" :rules="addRules" ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click='editCate'>确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类的对话框 -->
    <el-dialog @closed='handleReset' title="添加文章分类" :visible.sync="addVisible" width="40%">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addFormRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addCate">添 加</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章的分类列表
      cateList: [],
      red: 'red',
      //修改的数据
      editForm: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      editVisible: false,


      // 控制添加对话框的显示与隐藏
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    remove (id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async (e) => {
        //then是选择确定会触发
        console.log(e);//confirm
        const { data: res } = await this.$http.delete('/my/cate/del', {
          params: { id }
        })

        // 判断是否删除成功
        if (res.code !== 0) return this.$message.error('删除分类失败！')
        this.$message.success('删除分类成功！')
        this.initCateList()
      }).catch(e => {
        //catch是选择取消会触发
        console.log(e);//cancel
      })


    },
    // 完成分类的编辑操作
    editCate () {
      // 1. 手动表单验证
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 2. 调用接口
        const ret = await this.$http.put('/my/cate/info', this.editForm)
        // 3. 关闭弹窗
        if (ret.data.code === 0) {
          // 编辑成功
          this.editVisible = false
          // 4. 刷新列表
          this.initCateList()
        }
      })
    },
    // 点击修改按钮，显示编辑弹窗
    async handleEdit (id) {
      // 调用接口获取分类的具体数据
      const ret = await this.$http.get('/my/cate/info', {
        params: { id }
      })
      if (ret.data.code === 0) {
        // 获取分类的数据
        this.editForm = ret.data.data
        // 控制弹窗的显示
        this.editVisible = true
      }
    },
    addCate () {
      //console.log(this.addForm);
      // 表单预校验
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 发起请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('添加分类失败！')
        this.$message.success('添加分类成功！')
        // 关闭对话框
        this.addVisible = false
        // 重新请求列表数据
        this.initCateList()
      })
    },
    // 获取文章分类列表数据
    async initCateList () {
      const ret = await this.$http.get('/my/cate/list')
      if (ret.data.code === 0) {
        // 获取数据成功
        this.cateList = ret.data.data

      }
    },
    //分开写重置不会报错
    edithandleReset () {
      this.$refs.editFormRef.resetFields()
    },
    // 重置表单
    handleReset () {
      this.$refs.addFormRef.resetFields()


    },
  },

  created () {
    this.initCateList()
  },


}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
